<?php
/**
 * HaB
 *
 * Gestor de contenidos de alta disponibilidad
 *
 * @package		HaB
 * @author		Oscar M. Lage <r0sk10@gmail.com>
 * @author		Marcos B.L. <marcosbl@gmail.com>
 * @version		1.0
 */

/**
 * blog.php
 *
 * Archivo modelo del módulo blog
 *
 * @package		HaB
 * @version		1.0
 */

/**
 * Recoge las categorias de base de datos
 *
 * @return $var as array
 */
function categorias()
{
	global $config;
	
	$comentario = "Todas las categorías ordenadas por nombre";
	$sql = "SELECT *
			FROM categories
			ORDER BY category ASC";

	// Resultados
	$res = db_query($sql, $comentario);
	$dat=db_fetch($res);

	// Construyendo array
	$e = array();
	do
	{
		array_push($e, $dat);
	}while($dat=db_fetch($res));
	$var = array ('categorias' => $e);

	return $var;
}

/**
 * Nube de tags
 */
function tagcloud()
{
	$comentario = "Número de tags que se usan en posts agrupados por id";
	$sql = "SELECT posts_tags.tag_id, count(tag_id) AS quantity, tags.tag
			FROM posts_tags
			INNER JOIN tags ON posts_tags.tag_id = tags.id
			GROUP BY tags.id ORDER BY tags.id;";

	// Resultados
	$res = db_query($sql, $comentario);

	// Construyendo array
	$e = array();
	$dat=db_fetch($res);
	do
	{
		array_push($e, $dat);
	}while($dat=db_fetch($res));
	$var = array ('tags' => $e);
	
	foreach($e as $t)
	{
		$tid=$t['tag_id'];
		$tag2[$tid] = $t['quantity'];
	}
	
	// Máximo y mínimo tamaño de fuente
	$max_size = 26; // max font size in pixels
	$min_size = 11; // min font size in pixels
	$max_qty = max(array_values($tag2));
	$min_qty = min(array_values($tag2));
	 
	// Encontrar valores del rango
	$spread = $max_qty - $min_qty;
	if ($spread == 0) { // we don't want to divide by zero
		$spread = 1;
	}
	 
	// Incremento de la fuente
	$tamano = array();
	$step = ($max_size - $min_size) / ($spread);
	foreach($tag2 as $key => $value)
	{
		$size = round($min_size + (($value - $min_qty) * $step));
		array_push($tamano, $size);
	}
	$sizes = array ('sizes' => $tamano);
	
	return array_merge($var, $sizes);
}

/**
 * Últimos comentarios
 */
function lastcomments($num='5')
{
	$comentario = "Últimos {$num} comentarios para el menú";
	$sql = "SELECT comments.id, comments.author, comments.author_email, comments.comment, posts.slug
			FROM comments
			
			LEFT JOIN posts ON comments.post_id = posts.id
			LEFT JOIN posts_languages ON comments.post_id = posts_languages.post_id
			
			WHERE comments.status = 1
			AND posts_languages.lang = '".$_SESSION['LANGUAGE']."'
	ORDER BY comments.created DESC
	LIMIT $num";

	// Obtenemos los datos
	$res=db_query($sql, $comentario);

	// Construyendo array
	$e = array();
	$dat=db_fetch($res);
	do
	{
		array_push($e, $dat);
	}while($dat=$dat=db_fetch($res));
	$var = array ('lastcomments' => $e);

	return $var;
}

?>